$('document').ready(function(){	
	$('#signed').datepicker({showButtonPanel: true});
	$('#signed').datepicker('option', {dateFormat: 'dd/mm/yy'});
	$('#begin').datepicker({showButtonPanel: true});
	$('#begin').datepicker('option', {dateFormat: 'dd/mm/yy'});
	$('#end').datepicker({showButtonPanel: true});
	$('#end').datepicker('option', {dateFormat: 'dd/mm/yy'});
	$('#expiry').datepicker({showButtonPanel: true});
	$('#expiry').datepicker('option', {dateFormat: 'dd/mm/yy'});
	$( 'textarea#description' ).ckeditor();
	$.validator.methods.date = function (value, element) {   		
        return validateDate(value);
    }
	$.validator.addMethod('compareEndDate',function(value,element){
		return validateDate(value) && compareDate();
	},end_date_invalid);
	$.validator.addMethod('integer', function(value, element, param) {
            return (value != 0) && (value == parseInt(value, 10));
    }, zero_number);
	$.validator.addMethod('isDate', function(value, element, param) {
            return validateDate($('#begin').val());
    }, invalid_date);
	$('#frmAdd').validate({
            errorClass      : 'error',
            errorElement	: 'span',
    		onfocusout		: false,
    		onkeyup			: false,
    		ignore			: 'input[type="text"]:hidden',
			rules:{
				national:{
					required:true					
				},
				league:{
					required: true					
				},
				fee:{
					required:true,
					integer: true
				},
                signed:{
					required:true,
					isDate:true
                },
                begin:{
					required:true,
					isDate:true
                },
				end:{
					required:true,
					compareEndDate:true
				},
				expiry:{
					isDate:true
				},
                description:{
					required:true,
                    minlength:VL_CONTENT_MINLENGTH
                },
				salary_in_week:{
					integer: true
				}                				
			},
			messages:{
				national:{
					required:national_required					
				},
				league:{
					required: league_required					
				},
				fee:{
					required:fee_required,
					 integer: fee_number
				},
                signed:{
					required:signed_required,
					isDate: invalid_date
                },
                begin:{
					required:begin_required,
					isDate: invalid_date
                },
				end:{
					required:end_required,
					compareEndDate:end_date_invalid
				},
				expiry:{
					isDate:invalid_date
				},
                description:{
					 required:description_required,
                    minlength:description_minlength
                },
				salary_in_week:{
					integer:fee_number
				}
			},
			errorPlacement: function(error, element) {
			       error.insertAfter(element); // change this to place error message elsewhere
			}			
	});  
	$('.autoload').change(function(){ loadContract(); });
});
function compareDate(){
	//if(validateDate($('#end').val())){ return false; }
	begin = Date.parse($('#begin').val());
	end = Date.parse($('#end').val());	
	if(end <= begin) return false;
	return true;
}
function loadContract() {
	national = $('#national').val();
	league = $('#league').val();
	if(national.length > 0 && league.length > 0){
		$.ajax({
			url:HOST_BACKEND+'ajax/contract',
			type:'POST',
			data:{n:national,l:league},
			success:function(data){
				if(data){
					data = JSON && JSON.parse(data) || $.parseJSON(data);
					$('#clubid').html('');
					option='';
					$.each(data, function(key, value){						
						option = option+'<option value="'+value.id+'">'+ value.name+'</option>';
					});
					$('#clubid').html(option);
				}
			},
			error:function(){}
		});
	}
}

